//
//  ModifyRollPWDViewController.m
//  NiuBanJinGold
//
//  Created by zf on 15/10/13.
//  Copyright © 2015年 zzjr. All rights reserved.
//

#import "ModifyRollPWDViewController.h"
#import "BCTextField.h"
@interface ModifyRollPWDViewController ()<UITextFieldDelegate , UIAlertViewDelegate>
@property (nonatomic , strong) NSArray *array_labelText;
@property (nonatomic , strong) NSArray *array_fieldText;
@end

@implementation ModifyRollPWDViewController

- (void)viewDidLoad {
    
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    
    self.navigationItem.title = @"修改转出密码";
    
    self.array_labelText = @[@"原始密码" , @"新密码", @"确认密码"];
    self.array_fieldText = @[@"请输入原始密码"  , @"请输入新密码" ,@"请输入新密码"];
    self.view.backgroundColor = BASECOLOR;
    for (int i = 0 ; i < 3; i++) {
        UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, MULTIPLYHEIGHT(70)* (i + 1) -  MULTIPLYHEIGHT(50) , self.view.frame.size.width, MULTIPLYHEIGHT(50))];
        view.backgroundColor = [UIColor whiteColor];
        [self.view addSubview:view];
        
        UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(MULTIPLYWIDTH(15), MULTIPLYHEIGHT(70)* (i + 1) -  MULTIPLYHEIGHT(50) , MULTIPLYWIDTH(80), MULTIPLYHEIGHT(50))];
        label.text = [self.array_labelText objectAtIndex:i];
        label.font = [UIFont systemFontOfSize:15.0];
        label.textColor = [UsefulUtil colorWithHexString:@"#434343"];
        [self.view addSubview:label];
        
        BCTextField *field = [[BCTextField alloc]initWithFrame:CGRectMake(MULTIPLYWIDTH(105), MULTIPLYHEIGHT(70)* (i + 1) -  MULTIPLYHEIGHT(50) , MULTIPLYWIDTH(190), MULTIPLYHEIGHT(50))];
        [field setRegx:@"^[0-9]{0,6}+(\\.[0-9]{0,2})?$"];
        field.placeholder = [self.array_fieldText objectAtIndex:i];
        field.font = [UIFont systemFontOfSize:15.0];
        field.tag = 100 + i;
        field.delegate = self;
        field.secureTextEntry = YES;
        field.keyboardType = UIKeyboardTypeNumberPad;
        [self.view addSubview:field];
    }
    
    
    UIButton *button_modifyPWD = [UIButton buttonWithType:UIButtonTypeSystem];
    button_modifyPWD.frame = CGRectMake(MULTIPLYWIDTH(15),  MULTIPLYHEIGHT(240), self.view.frame.size.width - MULTIPLYWIDTH(30), MULTIPLYHEIGHT(45));
    button_modifyPWD.backgroundColor = [UIColor redColor];
    [button_modifyPWD setTitle:@"提交" forState:UIControlStateNormal];
    [button_modifyPWD addTarget:self action:@selector(commitAction) forControlEvents:UIControlEventTouchUpInside];
    button_modifyPWD.tintColor = [UIColor whiteColor];
    button_modifyPWD.titleLabel.font = [UIFont systemFontOfSize:17.0 ];//粗体
    button_modifyPWD.layer.cornerRadius = 8;
    [self.view addSubview:button_modifyPWD];
    
    UISwipeGestureRecognizer *swipeGesture = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(swipeGestureAction)];
    [self.view addGestureRecognizer:swipeGesture];
    
}
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
    
    if (buttonIndex == 0) {
        
        [self.navigationController popViewControllerAnimated:YES];
    }
}

#pragma mark -swipeGestureAction
-(void)swipeGestureAction{
    [self.navigationController popViewControllerAnimated:YES];
}

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
    
    
    NSInteger existedLength = textField.text.length;
    NSInteger selectedLength = range.length;
    NSInteger replaceLength = string.length;
    if (existedLength - selectedLength + replaceLength > 6) {
        return NO;
    }
    
    return YES;
}
#pragma mark - commitAction
-(void)commitAction{
    NSLog(@"提交");
    UITextField *textField_oldPWD = (UITextField *)[self.view viewWithTag:100];
    UITextField *textField_newPWD = (UITextField *)[self.view viewWithTag:101];
    UITextField *textField_newPWD1 = (UITextField *)[self.view viewWithTag:102];
    
    if (textField_oldPWD.text.length != 6) {
        [UsefulUtil showAlertView:@"请输入6位原始数字密码"];
        
    }else {
        
        if (textField_newPWD.text.length != 6 ) {
            [UsefulUtil showAlertView:@"请输入6位新数字密码"];
        }else {
            if ( textField_newPWD1.text.length != 6) {
                
                [UsefulUtil showAlertView:@"请输入6位确认数字密码"];
            }
        }
    }
    
   
    if (textField_oldPWD.text.length == 6 && textField_newPWD.text.length == 6 && textField_newPWD1.text.length == 6) {
        NSLog(@"??????token = %@" , GETCURRENTUSERINOBYPRAMETERES(user_userToken));
        NSDictionary *dic_change_password = @{@"userId" : GETCURRENTUSERINOBYPRAMETERES(user_userId) , @"originalPassword" : textField_oldPWD.text , @"newPassword" :textField_newPWD.text , @"token" : GETCURRENTUSERINOBYPRAMETERES(user_userToken)};
        
        if ([textField_newPWD1.text isEqualToString:textField_newPWD.text]) {
            [[NETWorkingUtil  sharedNETWorking] zzjrMore_change_passwordByParmeteres:dic_change_password transferResultUsingBlock:^(id parsedData, NSError *error) {
                if (!error) {
                    
                    if ([parsedData[@"status"] integerValue] == 0) {
                        
                        if ([parsedData[@"msg"] isEqualToString:@"成功" ]) {
                            
                            [UsefulUtil showAlertView:parsedData[@"msg"]];
                            [self.navigationController popViewControllerAnimated:YES];
                        }else {
                            
                            [UsefulUtil showAlertView:parsedData[@"msg"]];
                        }

                    }else {
                        [UsefulUtil showAlertView:parsedData[@"msg"]];
                    }
                    
                }else {
                    [UsefulUtil showAlertView:@"遇到网络问题,请稍后重试!"];
                }
            }];
        }else{
            
            [UsefulUtil showAlertView:@"两次新密码不一致"];
        }
    }else {
        
        
    }
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
